package cn.gbase.jiangsu.data.transfer.action;

import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.QueryResult;
import org.nutz.dao.pager.Pager;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.Scope;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Attr;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;
import org.nutz.service.IdEntityService;

import cn.gbase.jiangsu.data.transfer.bean.CompanyUser;
import cn.gbase.jiangsu.data.transfer.bean.Product;
import cn.gbase.jiangsu.data.transfer.bean.ProductSortTree;
import cn.gbase.jiangsu.data.transfer.bean.ProductSortTreeNode;
import cn.gbase.jiangsu.data.transfer.bean.VCompanyCommunity;
import cn.gbase.jiangsu.data.transfer.service.ProductService;
import cn.gbase.jiangsu.data.transfer.service.ProductSortTreeNodeService;
import cn.gbase.jiangsu.data.transfer.util.PropertyFilter;
import cn.gbase.jiangsu.data.transfer.util.SystemContext;

@IocBean(args = { "refer:dao" })
@At("/communityServiceMange")
public class CommunityServiceMangeAction extends IdEntityService<ProductSortTreeNode> {

	public CommunityServiceMangeAction() {
		super();
	}

	public CommunityServiceMangeAction(Dao dao) {
		super(dao);
	}

	@Inject
	private ProductService productService;

	@Inject
	private ProductSortTreeNodeService productSortTreeNodeService;

	//读取当前物业公司的小区列表
	@At("/listCommunityService")
	@Ok("jsp:/communityServiceMange/listCommunityServiceMange")
	public Object list(@Param("p") int pageNum, @Param("numPerPage") int numPerPage, HttpServletRequest request,
			@Attr(scope = Scope.SESSION, value = SystemContext.COMPANYUSER) CompanyUser companyUser) {
		List<PropertyFilter> filters = PropertyFilter.buildFromHttpRequest(request);
		Cnd cnd = PropertyFilter.toCnd(filters, "id", "DESC");
		cnd.and("id", "=", companyUser.getCompanyId());
		Pager pager = this.dao().createPager((pageNum < 1) ? 1 : pageNum,
				(numPerPage < 1) ? SystemContext.PAGE_SIZE : numPerPage);
		List<VCompanyCommunity> list = this.dao().query(VCompanyCommunity.class, cnd, pager);
		if (pager != null) {
			pager.setRecordCount(this.dao().count(VCompanyCommunity.class, cnd));
		}
		return new QueryResult(list, pager);
	}

	//读取当前物业公司对应的小区服务列表
	@At("/listCommunityServiceMange")
	@Ok("jsp:/communityServiceMange/serviceList")
	public Object listCommunityServiceMange(@Param("p") int pageNum, @Param("numPerPage") int numPerPage,
			HttpServletRequest request,
			@Attr(scope = Scope.SESSION, value = SystemContext.COMPANYUSER) CompanyUser companyUser) {
		ProductSortTree productSortTree = this.dao().fetch(ProductSortTree.class,
				Cnd.where("company_id", "=", companyUser.getCompanyId()));
		String childIdStr = new CommunityServiceMangeAction().getAllChildTreeId(productSortTree.getId() + "",
				this.dao());
		List<PropertyFilter> filters = PropertyFilter.buildFromHttpRequest(request);
		Cnd cnd = PropertyFilter.toCnd(filters, "id", "DESC");
		cnd.and("product_sort", "in", childIdStr);
		Pager pager = this.dao().createPager((pageNum < 1) ? 1 : pageNum,
				(numPerPage < 1) ? SystemContext.PAGE_SIZE : numPerPage);
		List<Product> list = this.dao().query(Product.class, cnd, pager);
		if (pager != null) {
			pager.setRecordCount(this.dao().count(Product.class, cnd));
		}
		request.setAttribute("treeId", productSortTree.getId());
		return new QueryResult(list, pager);
	}

	public String getAllChildTreeId(String treeId, Dao dao) {
		List<ProductSortTreeNode> ProductSortTreeNodeList = dao.query(ProductSortTreeNode.class,
				Cnd.where("treeId", "=", treeId));
		String childIdStr = "";
		for (ProductSortTreeNode productSortTreeNode : ProductSortTreeNodeList) {
			childIdStr += productSortTreeNode.getId() + ",";
		}
		String childIdStrNew = childIdStr.substring(0, childIdStr.length() - 1);
		return childIdStrNew;
	}

	//服务增加预处理
	@At("/preCreateService/?")
	@Ok("jsp:/communityServiceMange/serviceInput")
	public void preCreateService(String treeId, HttpServletRequest request) {
		String json = productService.getTreeJsonDataForZTree(Integer.parseInt(treeId));
		UUID uuid = UUID.randomUUID();
		String idNew = uuid.toString();
		request.setAttribute("idNew", idNew);
		request.setAttribute("treedata", json);
	}

	//服务增加
	@At("/saveService")
	@Ok(">>:/communityServiceMange/listCommunityServiceMange")
	public void saveService(@Param("..") Product product, HttpServletRequest request,
			@Attr(scope = Scope.SESSION, value = SystemContext.COMPANYUSER) CompanyUser companyUser) {
		product.setProductSort(product.getNodeId());
		product.setStockNumber(99999999);
		product.setCompanyId(companyUser.getCompanyId());
		product.setPassStatus(1);
		product.setProductType(2);
		//默认价格为0
		//		product.setSellPrice(BigDecimal.valueOf(0d));
		this.dao().insert(product);
		request.setAttribute("message", "已增加");
	}

	//服务修改预处理
	@At("/preUpdateService/?")
	@Ok("jsp:/communityServiceMange/serviceUpdate")
	public Object preUpdateService(Integer id, HttpServletRequest request) {
		Product product = this.dao().fetch(Product.class, id);
		ProductSortTreeNode productSortTreeNode = this.dao().fetch(ProductSortTreeNode.class,
				Cnd.where("id", "=", product.getProductSort()));
		product.setNodeId(product.getProductSort());
		product.setSelectedNodeName(productSortTreeNode.getName());
		// 商品分类
		String json = productService.getTreeJsonDataForZTree(productSortTreeNode.getTreeId());
		request.setAttribute("treedata", json);
		request.setAttribute("idNew", id);
		return product;
	}

	//服务增加
	@At("/updateService")
	@Ok(">>:/communityServiceMange/listCommunityServiceMange")
	public void updateService(@Param("..") Product product, HttpServletRequest request,
			@Attr(scope = Scope.SESSION, value = SystemContext.COMPANYUSER) CompanyUser companyUser) {
		product.setProductSort(product.getNodeId());
		product.setStockNumber(99999999);
		product.setCompanyId(companyUser.getCompanyId());
		product.setPassStatus(1);
		productService.updateProduct(product);
		request.setAttribute("message", "已修改");
	}

	//服务增加
	@At("/deleteService/?")
	public void deleteService(String id) {
		this.dao().clear(Product.class, Cnd.where("id", "=", id));
	}

}
